package com.youlu.campus.entity.course;

import com.youlu.campus.entity.course.dto.UserOnTopRequest;
import com.youlu.campus.entity.course.entity.UserAllTopRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : UserAllTopRecordService
 * @description : [所有用户置顶类]
 * @createTime : [2023/1/11 15:32]
 */
@Service
@Slf4j
public class UserAllTopRecordService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public boolean recordOnTop(UserOnTopRequest request){
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(request.getUserId()).and("uniqueId").is(request.getUniqueId()));
        UserAllTopRecord record = mongoTemplate.findOne(query,UserAllTopRecord.class);
        if(record == null){
            // 获取最大优先级
            int priority = getMaxPriority(request.getUserId()) + 1;
            record = new UserAllTopRecord();
            record.setOnTop(true);
            record.setUserId(request.getUserId());
            record.setUniqueId(request.getUniqueId());
            record.setPriority(priority);
            record.setCreatedTime(new Date());
            record.setUpdatedTime(new Date());
            mongoTemplate.insert(record);
            return true;
        }
        Update update = new Update();
        if(record.getOnTop()){
            update.set("onTop",false);
            update.set("priority",0);
        }else{
            // 获取最大优先级
            int priority = getMaxPriority(request.getUserId()) + 1;
            update.set("onTop",true);
            update.set("priority",priority);
        }
        update.setOnInsert("createdTime",new Date());
        update.set("updatedTime",new Date());
        update.setOnInsert("userId",request.getUserId());
        update.setOnInsert("uniqueId",request.getUniqueId());
        mongoTemplate.findAndModify(query,update,UserAllTopRecord.class);
        return true;
    }


    public Pair<Long,List<UserAllTopRecord>> getUserTopRecordList(String userId,List<String> ids){
        if(StringUtils.isBlank(userId)){
            return Pair.of(0L,new ArrayList<>());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("onTop").is(true));
        if(CollectionUtils.isNotEmpty(ids)){
            query.addCriteria(Criteria.where("uniqueId").is(ids));
        }
        long total = mongoTemplate.count(query,UserAllTopRecord.class);
        query.with(Sort.by("priority").descending());
        List<UserAllTopRecord> record = mongoTemplate.find(query,UserAllTopRecord.class);
        return Pair.of(total,record);
    }

    public int getMaxPriority(String userId){
        // 获取最大优先级
        Query query1 = new Query().addCriteria(Criteria.where("userId").is(userId));
        query1.with(Sort.by("priority").descending());
        UserAllTopRecord max = mongoTemplate.findOne(query1,UserAllTopRecord.class);
        return max == null ? 0 : max.getPriority();
    }
}
